package cn.hdu.liang.core.controller;

import cn.hdu.liang.common.encode.Md5Pwd;
import cn.hdu.liang.common.web.ResponseUtils;
import cn.hdu.liang.common.web.session.SessionProvider;
import cn.hdu.liang.core.bean.country.City;
import cn.hdu.liang.core.bean.country.Province;
import cn.hdu.liang.core.bean.country.Town;
import cn.hdu.liang.core.bean.user.Buyer;
import cn.hdu.liang.core.query.country.CityQuery;
import cn.hdu.liang.core.query.country.TownQuery;
import cn.hdu.liang.core.service.country.CityService;
import cn.hdu.liang.core.service.country.ProvinceService;
import cn.hdu.liang.core.service.country.TownService;
import cn.hdu.liang.core.service.user.BuyerService;
import cn.hdu.liang.core.web.Constants;
import com.octo.captcha.service.image.ImageCaptchaService;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 跳转登录页面
 * 登录
 * 个人资料
 * 收货地址
 */
@Controller
public class ProfileController {

    @Autowired
    private SessionProvider sessionProvider;
    @Autowired
    private Md5Pwd md5Pwd;
    @Autowired
    private BuyerService buyerService;
    @Autowired
    private ImageCaptchaService imageCaptchaService;
    @Autowired
    private ProvinceService provinceService;
    @Autowired
    private CityService cityService;
    @Autowired
    private TownService townService;

    //GET
    @RequestMapping(value = "/shopping/login.shtml", method = RequestMethod.GET)
    public String login() {
        return "buyer/login";
    }

    //POST
    @RequestMapping(value = "/shopping/login.shtml", method = RequestMethod.POST)
    public String login(Buyer buyer, String captcha, String returnUrl, ModelMap model, HttpServletRequest req) {
        if(StringUtils.isNotBlank(captcha)) {
            String sessionId = sessionProvider.getSessionId(req);
            if(imageCaptchaService.validateResponseForID(sessionId, captcha)) {
                if(buyer != null && StringUtils.isNotBlank(buyer.getUsername())) {
                    if(StringUtils.isNotBlank(buyer.getPassword())) {
                        Buyer user = buyerService.getBuyerByKey(buyer.getUsername());
                        if(user != null) {
                            if(user.getPassword().equals(md5Pwd.encode(buyer.getPassword()))) {
                                //用户对象放在Session中
                                sessionProvider.setAttribute(req, Constants.BUYER_SESSION, user);
                                if(StringUtils.isNotBlank(returnUrl)) {
                                    return "redirect:" + returnUrl;
                                } else {
                                    return "redirect:/buyer/index.shtml";
                                }
                            } else {
                                model.addAttribute("error", "密码输入错误");
                            }
                        } else {
                            model.addAttribute("error", "用户名输入错误");
                        }
                    } else {
                        model.addAttribute("error", "请输入密码");
                    }
                } else {
                    model.addAttribute("error", "请输入用户名");
                }
            } else {
                model.addAttribute("error", "验证码错误");
            }
        } else {
            model.addAttribute("error", "请填写验证码");
        }
        return "buyer/login";
    }

    //个人中心
    @RequestMapping(value = "/buyer/index.shtml")
    public String index() {
        return "buyer/index";
    }

    //个人资料
    @RequestMapping(value = "/buyer/profile.shtml")
    public String profile(HttpServletRequest req, ModelMap model) {
        //加载用户
        Buyer buyer = (Buyer) sessionProvider.getAttribute(req, Constants.BUYER_SESSION);
        Buyer user = buyerService.getBuyerByKey(buyer.getUsername());
        model.addAttribute("buyer", user);

        //省
        List<Province> provinces = provinceService.getProvinceList(null);
        model.addAttribute("provinces", provinces);
        //市
        CityQuery cityQuery = new CityQuery();
        cityQuery.setProvince(user.getProvince());
        List<City> citys = cityService.getCityList(cityQuery);
        model.addAttribute("citys", citys);
        //县
        TownQuery townQuery = new TownQuery();
        townQuery.setCity(user.getCity());
        List<Town> towns = townService.getTownList(townQuery);
        model.addAttribute("towns", towns);

        return "buyer/profile";
    }

    //收货地址
    @RequestMapping(value = "buyer/deliver_address.shtml")
    public String address() {
        return "buyer/deliver_address";
    }

    //加载城市
    @RequestMapping(value = "/buyer/city.shtml")
    public void city(String code, HttpServletResponse resp) {
        CityQuery cityQuery = new CityQuery();
        cityQuery.setProvince(code);
        List<City> citys = cityService.getCityList(cityQuery);
        JSONObject json = new JSONObject();
        json.put("citys", citys);
        ResponseUtils.renderJson(resp,json.toString());
    }

    //加载县/区
    @RequestMapping(value = "/buyer/town.shtml")
    public void town(String code, HttpServletResponse resp) {
        TownQuery townQuery = new TownQuery();
        townQuery.setCity(code);
        List<Town> towns = townService.getTownList(townQuery);
        JSONObject json = new JSONObject();
        json.put("towns", towns);
        ResponseUtils.renderJson(resp,json.toString());
    }
}
